import WxValidate from "../../../utils/wx-validate/WxValidate";
import Promise from '../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime'
const utils = require('../../../utils/util.js');
const app=getApp();

Page({
  data: {
    times:"",
    changeList:[
      {
        date:"2018 01 01",
        time:"12:30:56",
        content:"充值100元"
      },
      {
        date:"2018 01 01",
        time:"12:30:56",
        content:"充值100元"
      },
      {
        date:"2018 01 01",
        time:"12:30:56",
        content:"充值100元"
      }
    ],
    orderList:[],
    page:1,
    pageSize:100,
    getOrderListUrl:"/api/Trade/getOrdList",
    orderUrl:"/api/Trade/createOrder",
    delOrder:"/api/Trade/delOrder"
  },
  onLoad: async function() {
    wx.showLoading({
      title:"加载中..."
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    const result = await this._getOrderList();
    wx.hideLoading();
    if(result.state){
      var olist=result.list;
      var chargeList=olist.charge_list;
      var appment_left=olist.appment_left;
      var res_code=olist.res_code;
      this.setData({
        orderList:olist.data,
        chargeList:chargeList,
        times:appment_left,
        res_code:res_code
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  _getOrderList:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.getOrderListUrl,{token:this.data.token,page:this.data.page,pageSize:this.data.pageSize},res=>{
          //console.log(res);
          var data=res.data;
          if(data.state){
            resolve({state:true,list:data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  },
  chargeTap: async function(e) {
    var count=e.target.dataset.count;
    console.log(count);
    wx.showLoading({
      title:"请稍后..."
    })
    const result = await this._setPay(count);
    wx.hideLoading();
    if(result.state){
      var data=result.data;
      var payReslut= await this._wxPay(data);
      if(payReslut.state){
        wx.showToast({
          title:payReslut.msg,
          icon:"none",
          duration:1000
        })
        this.setData({
          page:1
        })
        this.onLoad();
      }
     else{
        var delOrder=await this._delOrder(data.out_trade_no);
        wx.showToast({
          title:payReslut.msg,
          icon:"none",
          duration:1000
        })
      }
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  _setPay:function(count){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      app.request(this.data.orderUrl,{token:this.data.token,apply_count:count},res=>{
        //console.log(res);
        var data=res.data;
        if(data.state){
          resolve({state:true,data:data});
        }
        else{
          resolve({state:false,msg:data.msg});          
        }
      },error=>{
        resolve({state:false,msg:"操作失败，请重试"});
      })
    });
    return p;
  },
  _wxPay:function(data){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      wx.requestPayment({
        'timeStamp': data.timeStamp,
        'nonceStr': data.nonceStr,
        'package': data.package,
        'signType': 'MD5',
        'paySign': data.paySign,
        'success': function (res) {
          console.log("支付成功！");
          resolve({state:true,msg:"支付成功"});
        },
        'fail': function (res) {
          console.log("支付失败！");
          resolve({state:false,msg:"支付失败！"});
        }
      })
    });
    return p;
  },
  _delOrder:function(out_trade_no){
    var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      app.request(this.data.delOrder,{token:this.data.token,out_trade_no:out_trade_no},res=>{
        //console.log(res);
        var data=res.data;
        if(data.state){
          resolve({state:true,data:data.data});
        }
        else{
          resolve({state:false,msg:data.msg});          
        }
      },error=>{
        resolve({state:false,msg:"操作失败，请重试"});
      })
    });
    return p;
  },
  onShareAppMessage:function(){
    var obj={
      title:"快来加入办卡小程序的大家庭把~",
      path:"/pages/index/index?res_code="+this.data.res_code,
      imageUrl:"/images/start.jpg"
    }
    return obj;
  }
})
